package com.elf.stepcounter;

import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.TextView;

import androidx.appcompat.app.ActionBar;
import androidx.appcompat.widget.Toolbar;

import com.elf.zero.app.BaseActivity;

public class AppBaseActivity extends BaseActivity {

    ActionBar actionBar;
    TextView tv_title;

    @Override
    public void setContentView(int layoutResID) {
        super.setContentView(layoutResID);
        initToolbar();
    }

    @Override
    public void setContentView(View view) {
        super.setContentView(view);
        initToolbar();
    }

    private void initToolbar(){
        Toolbar toolbar = findViewById(R.id.toolbar);
        if (toolbar != null) {
            setSupportActionBar(toolbar);
        }
        actionBar = getSupportActionBar();
        if (actionBar != null) {
            actionBar.setDisplayHomeAsUpEnabled(true);
            actionBar.setTitle("");
        }

        tv_title = findViewById(R.id.tv_title);

        setToolTitleCenter(toolbar);
    }

    @Override
    public void setTitle(int titleId) {
        if (tv_title != null) {
            tv_title.setText(titleId);
        }
    }

    @Override
    public void setTitle(CharSequence title) {
        if (tv_title != null) {
            tv_title.setText(title);
        }
    }

    /**
     * 隐藏主页或返回键
     */
    protected void hideHomeAsUp() {
        if (actionBar != null) {
            actionBar.setDisplayHomeAsUpEnabled(false);
        }
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        int resId = getMenuResId();
        if (resId > 0) {
            getMenuInflater().inflate(getMenuResId(), menu);
        }
        optionsMenu(menu);
        return super.onCreateOptionsMenu(menu);
    }

    protected void optionsMenu(Menu menu) {

    }

    protected int getMenuResId() {
        return 0;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {

        menuSelected(item);

        return super.onOptionsItemSelected(item);
    }

    protected void menuSelected(MenuItem item) {

        if (item.getItemId() == android.R.id.home) {
            finish();
        }
    }
}
